package com.qdu.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/ls")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1.获取请求参数
        String uname = req.getParameter("username");
        String pwd = req.getParameter("password");
        String rem = req.getParameter("rem");

        Cookie cookie1 = new Cookie("username",req.getParameter("username"));
        Cookie cookie2 = new Cookie("password",req.getParameter("password"));

        if("Y".equals(rem)) {
            cookie1.setMaxAge(24*60);
            cookie2.setMaxAge(24*60);
        }
        else {
            cookie1.setMaxAge(0);
            cookie2.setMaxAge(0);
        }
        resp.addCookie(cookie1);
        resp.addCookie(cookie2);


        //验证登录，本程序是不管账户和密码是否正确，如果勾选了“记住密码”，都会记住账户和密码
        if ("anna".equals(uname) && "1234".equals(pwd)) {
            resp.sendRedirect("success.jsp");
        } else {
            resp.sendRedirect("fail.html");
        }
    }
}
